<template>
	<view class="tui-addr-box">
		
		<form :report-submit="true">
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">会员昵称</view>
					<view class="tui-title">{{myInfo.name}}</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">手机号码</view>
					<view class="tui-title">{{myInfo.phone}}</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">水票类型</view>
					<view class="tui-title" v-if="shipType == 'ship'">商家配送</view>
					<view class="tui-title" v-else>用户自提</view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">剩余水票</view>
					<view class="tui-title" v-if="shipType == 'ship'">{{myTicket.ship_last_num}}</view>
					<view class="tui-title" v-else>{{myTicket.self_last_num}}</view>
				</view>
			</tui-list-cell>
			
			
			<view class="tui-top">
				<tui-list-cell :last="false" :radius="1">
					<view class="tui-flex">
						<view class="tui-balance">配送地址<text class="tui-gray"></text></view>
					</view>
				</tui-list-cell>
			</view>
			
			<tui-list-cell :arrow="true" :last="true" :radius="1" @click="goMyAddressIndex()">
				<view class="tui-address">
					<view v-if="(myAddressId > 0)">
						<view class="tui-userinfo">
							<text class="tui-name">{{myAddressInfo.name}}</text> {{myAddressInfo.phone}}
						</view>
						<view class="tui-addr">
							<view v-if="(myAddressInfo.is_home == 1)" class="tui-addr-tag">默认</view>
							<!-- <view class="tui-addr-tag">公司</view> -->
							<text>{{myAddressInfo.address_all}}</text>
						</view>
					</view>
					<view class="tui-none-addr" v-else>
						<image src="/static/images/index/map.png" class="tui-addr-img" mode="widthFix"></image>
						<text>选择收货地址</text>
					</view>
				</view>
				<view class="tui-bg-img"></view>
			</tui-list-cell>
			
			<view class="tui-top">
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">配送数目</view>
					<input placeholder-class="tui-phcolor" class="tui-input" name="apply_num" v-model="apply_num" placeholder="需要几桶水?" maxlength="6" type="number" />
				</view>
			</tui-list-cell>
			</view>
					
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">申请说明</view>
					<input placeholder-class="tui-phcolor" class="tui-input" name="remark" v-model="remark" placeholder="配送备注说明" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			
			<!-- 保存地址 -->
			<view class="tui-addr-save" @click="postToApply()">
				<tui-button type="green" height="88rpx">确认呼叫送水</tui-button>
			</view>
			
			<view class="tui-protocol" hover-class="opcity" :hover-stay-time="150">
				<text class="tui-protocol-red">送水站收到你的信息会尽快安排配送~</text>
				<br><br>
				<text class="tui-protocol-red">请耐心等待，谢谢配合</text>
				<br><br><br><br><br><br>
			</view>
		</form>
		
		<footer-menu :tabCur="2"></footer-menu>
	</view>
</template>

<script>
	import footerMenu from "@/components/footer-menu.vue"
	import tuiIcon from "@/components/thorui/tui-icon/tui-icon.vue"
	import tuiButton from "@/components/thorui/tui-button/tui-button"
	import tuiListView from "@/components/thorui/tui-list-view/tui-list-view"
	import tuiListCell from "@/components/thorui/tui-list-cell/tui-list-cell"
	const util = require('../../utils/util.js')
	export default {
		components: {
			footerMenu,
			tuiButton,
			tuiListView,
			tuiListCell,
			tuiIcon
		},
		data() {
			return {
				myName: '',
				myPhone: '',
				myMoneyGold: '',
				apply_phone: '',
				apply_num: 1,
				remark: '',
				
				myInfo:{},
				myTicket:{},
				
				myAddressId: 0,		//选择的地址
				myAddressInfo: [],
				
				shipType: 'ship',
				
			}
		},
		methods: {
			
			goMyAddressIndex: function() {
				uni.redirectTo({
					url: "/pages/my-address/index?jump_url=/pages/shop/ticket-apply"
				})
			},
			
			//我的个人信息
			getMyBaseInfo: function() {
				uni.request({
					url: this.$apiUrl+ '/api/user/baseInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					success: (res) => {
						let resErr = res.data['error'];
						let resMsg = res.data['msg'];
						let resCode = res.data['code'];
						if(resCode == 401) {		//用户未登录
							uni.navigateTo({ url: '/pages/my/login' });
						}else if(resCode == 200){
							let resData = res.data['data'];
							this.myInfo = resData;
						}else{
							this.tui.toast(resMsg, 2000, false);
						}
					}
				});
			},
			
			//我的优先地址
			getMyAddressBest: function(addressId) {
				uni.request({
					url: this.$apiUrl + '/api/user/addressInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					data: {
						'address_id': addressId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resError = res.data['error'];
						if(resError == 0) {
							this.myAddressId = resData.id;
							this.myAddressInfo = resData;
						}else{
							uni.setStorageSync('userAddressId', 0);
						}
					}
				});
			},
			
			
			//我的个人信息
			getMyTicketInfo: function() {
				uni.request({
					url: this.$apiUrl+ '/api/user/ticketInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					success: (res) => {
						let resErr = res.data['error'];
						let resData = res.data['data'];
						this.myTicket = resData;	
					}
				});
			},
			
			//确认叫水
			postToApply: function() {
				if (util.isNullOrEmpty(this.myAddressId)) { this.tui.toast('请选择配送地址'); return } 
				if (util.isNullOrEmpty(this.apply_num)) { this.tui.toast('请填写送水的数目'); return } 
				if(this.shipType == 'ship') {
					if(Number(this.apply_num) > Number(this.myTicket.ship_last_num)) { this.tui.toast('你的当前水票不足'); return } 
				}
			
				if(this.shipType == 'self') {
					if(Number(this.apply_num) > Number(this.myTicket.self_last_num)) { this.tui.toast('你的当前水票不足'); return } 
				}
				
				if (util.isNullOrEmpty(this.remark)) { this.tui.toast('请输入申请说明'); return } 
				
				uni.request({
					url: this.$apiUrl + '/api/user/ticketToApply',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId'),
					},
					data: {
						apply_num: this.apply_num,
						address_id: this.myAddressId,
						remark: this.remark,
						type: this.shipType,
					},
					success: (res) => {
						let resMsg = res.data['msg'];
						let resErr = res.data['error'];
						if(resErr == 0 ){		//发送成功的时候
							this.tui.toast(resMsg);
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/shop/ticket-index'
								});
							}, 2000);
						}else{
							this.tui.toast(resMsg);
						}
					}
				});
			},
			
		},
		
		onLoad: function(options) {
			this.shipType = options.type || 'ship';
			this.getMyBaseInfo();
			this.getMyTicketInfo();
			
			this.myAddressId = uni.getStorageSync('userAddressId') || 0;
			if(this.myAddressId) {
				this.getMyAddressBest(this.myAddressId);
			}
		},
		
	}
</script>

<style>
	
	.tui-addr-box {
		padding: 20rpx 0;
	}

	.tui-line-cell {
		width: 100%;
		padding: 24rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-title {
		width: 180rpx;
		font-size: 28rpx;
	}

	.tui-title-city-text {
		width: 180rpx;
		height: 40rpx;
		display: block;
		line-height: 46rpx;
	}

	.tui-input {
		width: 500rpx;
	}

	.tui-input-city {
		flex: 1;
		height: 40rpx;
		font-size: 28rpx;
		padding-right: 30rpx;
	}

	.tui-phcolor {
		color: #ccc;
		font-size: 28rpx;
	}
	.tui-cell-title{
		font-size: 28rpx;
	}
	.tui-addr-label {
		margin-left: 70rpx;
	}

	.tui-label-item {
		width: 76rpx;
		height: 40rpx;
		border: 1rpx solid rgb(136, 136, 136);
		border-radius: 6rpx;
		font-size: 26rpx;
		text-align: center;
		line-height: 40rpx;
		margin-right: 20rpx;
		display: inline-block;
		transform: scale(0.9);
	}
	.tui-label-active{
		background: #E41F19;
		border-color:#E41F19;
		color: #fff;
	}
	.tui-swipe-cell {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		padding: 10rpx 24rpx;
		border-radius: 6rpx;
		overflow: hidden;
		font-size: 28rpx;
	}
	
	.tui-addr-save {
		padding: 24rpx;
		margin-top: 100rpx;
	}
	
	
	.tui-protocol {
		color: #333;
		font-size: 24upx;
		text-align: center;
		width: 100%;
		margin-top: 29upx;
	}
	
	.tui-protocol-red {
		color: #f54f46;
	}
	
	
	
	.tui-address {
		min-height: 80rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
		position: relative;
	}
	
	.tui-userinfo {
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
		padding-bottom: 12rpx;
	}
	
	.tui-name {
		padding-right: 40rpx;
	}
	
	.tui-addr {
		font-size: 24rpx;
		word-break: break-all;
		padding-right: 25rpx;
	}
	
	.tui-addr-tag {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #EB0909;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: 0 center;
		border-radius: 6rpx;
	}
	
	.tui-bg-img {
		position: absolute;
		width: 100%;
		height: 8rpx;
		left: 0;
		bottom: 0;
		background: url("") repeat;
	}
	
	.tui-top {
		margin-top: 20rpx;
		overflow: hidden;
	}
	
	.tui-goods-title {
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}
	
	.tui-padding {
		box-sizing: border-box;
	}
	
	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}
	
	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
		border-radius: 6rpx;
	}
	
	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}
	
	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}
	
	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}
	
	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}
	
	.tui-flex {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
	}
	.tui-total-flex{
		justify-content: flex-end;
	}
	
	.tui-color-red,
	.tui-invoice-text {
		color: #E41F19;
		padding-right: 30rpx;
	}
	
	.tui-balance {
		font-size: 28rpx;
		font-weight: 500;
	}
	
	.tui-black {
		color: #222;
		line-height: 30rpx;
	}
	
	.tui-gray {
		color: #888888;
		font-weight: 400;
	}
	
	.tui-light-dark {
		color: #666;
	}
	
	.tui-goods-price {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
	}
	
	.tui-size-26 {
		font-size: 26rpx;
		line-height: 26rpx;
	}
	
	.tui-price-large {
		font-size: 34rpx;
		line-height: 32rpx;
		font-weight: 600;
	}
	
	.tui-flex-end {
		display: flex;
		align-items: flex-end;
		padding-right: 0;
	}
	
	.tui-phcolor {
		color: #B3B3B3;
		font-size: 26rpx;
	}
	
	/* #ifndef H5 */
	.tui-remark-box {
		padding: 22rpx 30rpx;
	}
	
	/* #endif */
	/* #ifdef H5 */
	.tui-remark-box {
		padding: 26rpx 30rpx;
	}
	
	/* #endif */
	
	.tui-remark {
		flex: 1;
		font-size: 26rpx;
		padding-left: 64rpx;
	}
	
	.tui-scale-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}
	
	.tui-scale-small .wx-switch-input {
		margin: 0 !important;
	}
	
	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}
	
	/* #endif */
	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 999;
	}
	
	.tui-pr-30 {
		padding-right: 30rpx;
	}
	
	.tui-pr-20 {
		padding-right: 20rpx;
	}
	
	.tui-none-addr {
		height: 80rpx;
		padding-left: 5rpx;
		display: flex;
		align-items: center;
	}
	
	.tui-addr-img {
		width: 36rpx;
		height: 46rpx;
		display: block;
		margin-right: 15rpx;
	}
	
	.tui-pr25 {
		padding-right: 25rpx;
	}
	
	.tui-safe-area {
		height: 1rpx;
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>